import axios from 'axios'

const service = axios.create({
    baseURL: 'http://120.92.101.187/index.php/index',
    timeout: 15000
})

// 请求拦截器
service.interceptors.request.use(
    config => {
        // 可在此统一添加token
        // const token = localStorage.getItem('token')
        // if (token) {
        //     config.headers['token'] = token
        // }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// 响应拦截器
service.interceptors.response.use(
    response => {
        const res = response.data
        // 根据接口文档，code为0表示成功
        if (res.code !== 0) {
            return Promise.reject(new Error(res.msg || 'Error'))
        }
        return res.data // 直接返回数据部分
    },
    error => {
        return Promise.reject(error)
    }
)

// 封装GET方法
function get(url, params, options = {}) {
    return service({
        url,
        params,
        method: 'GET',
        ...options
    })
}

// 封装POST方法
function post(url, data, options = {}) {
    return service({
        url,
        data,
        method: 'POST',
        ...options
    })
}
//获取教师列表
function getlist(url, params, options = {}) {
    return service({
        url,
        params,
        method: 'GET',
        ...options
    })
}
//添加讲师
function add(url, data, options = {}) {
    return service({
        url,
        data,
        method: 'POST',
        ...options
    })
}
//删除
function del(url, params, options = {}) {
    return service({
        url,
        params,
        method: 'GET',
        ...options
    })
}
//查看详情
function details(url, params, options = {}) {
    return service({
        url,
        params,
        method: 'GET',
        ...options
    })
}
//修改
function updata(url, data, options = {}) {
    return service({
        url,
        data,
        method: 'POST',
        ...options
    })
}
//搜索
function search(url, params, options = {}) {
    return service({
        url,
        params,
        method: 'GET',
        ...options
    })
}
export default {
    get,
    post,
	getlist,
	add,
	del,
	details,
	updata,
	search
}
